package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exceptions.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo> implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    //图片列表
    @Autowired
    private GraphInfoService graphInfoService;
    //配套列表
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    //标签列表
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    //杂费列表
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    //省份
    @Autowired
    private ProvinceInfoService provinceInfoService;
    //城市
    @Autowired
    private CityInfoService cityInfoService;
    //区县
    @Autowired
    private DistrictInfoService districtInfoService;
    //房间
    @Autowired
    private RoomInfoService roomInfoService;

    //图片
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    //配套
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    //标签
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    //杂费
    @Autowired
    private FeeValueMapper feeValueMapper;

    //
    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {

        //设置省份，城市，区县的名称  通过 apartmentSubmitVo的ID获得名称
        //apartmentSubmitVo.getId给provinceInfoService。getById()获得省份对象，然后getName(),
        // 然后set进apartmentSubmitVo的省份Name中
        //城市与区县的相同
        apartmentSubmitVo.setProvinceName(provinceInfoService.getById(apartmentSubmitVo.getProvinceId()).getName());
        apartmentSubmitVo.setCityName(cityInfoService.getById(apartmentSubmitVo.getCityId()).getName());
        apartmentSubmitVo.setDistrictName(districtInfoService.getById(apartmentSubmitVo.getDistrictId()).getName());
        //修改均为删除后保存；有ID为修改反之为保存

        //判断公寓ID是否为空
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        saveOrUpdate(apartmentSubmitVo);
        //1.图片删除
        //条件：
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 1.图片的item_type是否等于枚举类型的公寓为1：ItemType.APARTMENT
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        // 2.图片的item_id是否等于公寓ID
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
        //IService的remove方法
        graphInfoService.remove(graphInfoLambdaQueryWrapper);


        //2.配套删除
        //条件
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //配套关系表中的apartment_id与公寓ID是否相等
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
        //IService的remove方法
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);


        //3.标签删除
        //条件
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //标签关系表中的apartment_id与公寓ID是否相等
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
        //IService的remove方法
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);


        //4.杂费删除
        //条件
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        //杂费关系表中的apartment_id与公寓ID是否相等
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
        //IService的remove方法
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);


        //保存图片
        //可能多个图片，所以用集合
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        //判断graphVoList集合是否为空
        if (!CollectionUtils.isEmpty(graphVoList)) {
            //GraphVo继承GraphInfo  多态 向上转型
            ArrayList<GraphInfo> graphInfoArrayList = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                //GraphVo里面的参数写到GraphInfo中
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                //枚举类型
                graphInfo.setItemType(ItemType.APARTMENT);
                //公寓的ID
                graphInfo.setItemId(apartmentSubmitVo.getId());
                //将graphInfo放到graphInfoArrayList集合中
                graphInfoArrayList.add(graphInfo);
            }
            //单表IService的批量插入saveBatch方法
            graphInfoService.saveBatch(graphInfoArrayList);
        }

        //保存配套
        //多条数据用集合
        List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();
        //判断集合是否为空
        if (!CollectionUtils.isEmpty(facilityInfoIdList)) {
            //多态 向上转型
            List<ApartmentFacility> apartmentFacilityList = new ArrayList<>(facilityInfoIdList.size());
            //ApartmentSubmitVo里面的参数写到配套关系表中
            for (Long facilityInfoId : facilityInfoIdList) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                        //将apartmentSubmitVo中的facilityInfoId写到配套联系表中的facilityId(facility_id)中
                        .facilityId(facilityInfoId)
                        //将apartmentSubmitVo中的公寓Id写到配套联系表中的apartmentId(apartment_id)中
                        .apartmentId(apartmentSubmitVo.getId())
                        .build();
                //将apartmentFacility对象add到apartmentFacilityList集合中
                apartmentFacilityList.add(apartmentFacility);
            }
            //单表IService的批量插入saveBatch方法
            apartmentFacilityService.saveBatch(apartmentFacilityList);
        }


        //保存标签
        //集合保存
        List<Long> labelIdList = apartmentSubmitVo.getLabelIds();
        //判断集合是否为空
        if (!CollectionUtils.isEmpty(labelIdList)) {
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>(labelIdList.size());
            //ApartmentSubmitVo里面的参数写到标签关系表中
            for (Long labelIds : labelIdList) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder()
                        //将labelIds写到标签联系表的labelId(label_id)中
                        .labelId(labelIds)
                        //将apartmentSubmitVo中的公寓ID写到标签联系表的apartmentId(apartment_id)中
                        .apartmentId(apartmentSubmitVo.getId())
                        .build();
                //将apartmentLabel对象add到apartmentLabelList集合中
                apartmentLabelList.add(apartmentLabel);
            }
            //单表IService的批量插入saveBatch方法
            apartmentLabelService.saveBatch(apartmentLabelList);
        }


        //保存杂费
        //集合保存
        List<Long> feeValueIdList = apartmentSubmitVo.getFeeValueIds();
        //集合是否为空
        if (!CollectionUtils.isEmpty(feeValueIdList)) {
            List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>(feeValueIdList.size());
            //ApartmentSubmitVo里面的参数写到杂费关系中
            for (Long feeValueIds : feeValueIdList) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                        //将feeValueIds写到杂费联系表的labelId(label_id)中
                        .feeValueId(feeValueIds)
                        //将apartmentSubmitVo中的公寓ID写到杂费联系表的apartmentId(apartment_id)中
                        .apartmentId(apartmentSubmitVo.getId())
                        .build();
                //将apartmentFeeValue对象add到apartmentFeeValueList集合中
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            //单表IService的批量插入saveBatch方法
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }
    }


    @Override
    public void customPage(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        apartmentInfoMapper.customSelectPage(page, queryVo);
    }

    @Override
    public ApartmentDetailVo customDetail(Long id) {
        //  1. 先根据公寓id查询公寓详情对象
        ApartmentInfo apartmentInfo = getById(id);
        //  2. 根据公寓id查询图片的vo对象 (定义mapper方法和mapper方法调用)
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphVoList(id,ItemType.APARTMENT);
        //  3. 根据公寓id查询配套的对象  (定义mapper方法和mapper方法调用)
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.customSelectList(id);
        //  4. 根据公寓id查询标签的对象  (定义mapper方法和mapper方法调用)
        List<LabelInfo> labelInfoList = labelInfoMapper.customSelectList(id);
        //  5. 根据公寓id查询杂费的Vo对象 (定义mapper方法和mapper方法调用)
        List<FeeValueVo> feeValueVoList = feeValueMapper.customSelectList(id);
        //  6. 将数据合并到结果Vo中返回即可
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        //集体赋值 等价于copy
        //从原对象到目标对象
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        return apartmentDetailVo;
    }

    @Override
    public void customRemoveById(Long id) {
        //判断公寓里面是否有房间
        //条件
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //看room_info表中是否存在本公寓ID
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        long count = roomInfoService.count(roomInfoLambdaQueryWrapper);
        if (count > 0){
            //表示公寓里面还有房间，即不能进行删除
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR.getMessage(),
                    ResultCodeEnum.SERVICE_ERROR.getCode());
        }
        //没有正常删除，IService的removeById方法
        removeById(id);
        //1.图片删除
        //条件：
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 1.图片的item_type是否等于枚举类型的公寓为1：ItemType.APARTMENT
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        // 2.图片的item_id是否等于公寓ID
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        //IService的remove方法
        graphInfoService.remove(graphInfoLambdaQueryWrapper);


        //2.配套删除
        //条件
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //配套关系表中的apartment_id与公寓ID是否相等
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        //IService的remove方法
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);


        //3.标签删除
        //条件
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //标签关系表中的apartment_id与公寓ID是否相等
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        //IService的remove方法
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);


        //4.杂费删除
        //条件
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        //杂费关系表中的apartment_id与公寓ID是否相等
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        //IService的remove方法
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

    }
}




